<template>
  <div class="login-container">
    <div class="login-form-content">
      <div class="login-bg">
        <div class="header s-d-flex s-jc-space-between s-ai-center">
          <div class="flex-1 fz20 title">忘记密码</div>
          <div class="flex-1 back s-pointer s-tar" @click="backLogin">
            返回登录
          </div>
        </div>
        <div>
          <a-form
            ref="formRef"
            :model="formState"
            :rules="rules"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
            style="display: flex; flex-wrap: wrap"
          >
            <a-form-item
              style="width: 100%"
              ref="mobile"
              label="手机号"
              name="mobile"
            >
              <a-input
                v-model:value="formState.mobile"
                placeholder="请输入手机号"
              />
            </a-form-item>

            <a-form-item
              style="width: 100%"
              ref="randomValidateCode"
              label="图形验证码"
              name="randomValidateCode"
            >
              <a-input
                v-model:value="formState.randomValidateCode"
                placeholder="图形验证码"
              >
                <template #suffix>
                  
                  <img
                    style="width: 120px; height: 30px; margin-top: 5px"
                    @click="handleGetRandomValidateCode"
                    :src="src"
                    alt=""
                  />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item
              style="width: 100%"
              ref="smsCode"
              label="短信验证码"
              name="smsCode"
            >
              <a-input
                v-model:value="formState.smsCode"
                placeholder="请输入短信验证码"
              >
                 <template #suffix> 
                  <span @click="getCode" style="cursor:pointer;color: #34bbff;">
                     {{showTime?`${timer}s重新获取验证码`:'获取验证码'}}
                    </span> </template>
              </a-input>
            </a-form-item>

            <a-form-item
              ref="password"
              label="新密码"
              name="password"
              style="width: 100%"
            >
              <a-input
                v-model:value="formState.password"
                placeholder="请输入6-20位登录密码"
              />
            </a-form-item>
            <a-form-item
              style="width: 100%"
              ref="passwordComfirm"
              label="确认新密码："
              name="passwordComfirm"
            >
              <a-input
                v-model:value="formState.passwordComfirm"
                placeholder="再次输入新密码"
              />
            </a-form-item>

            <a-form-item style="width: 100%; margin-left: 150px">
              <a-button type="primary" block @click="handleSave">确认</a-button>
            </a-form-item>
          </a-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  defineComponent,
  onMounted,
  reactive,
  ref,
  toRaw,
  toRef,
  toRefs,
  watch,
} from "vue";
import * as userApi from "@/api/user";
import { EnvironmentTwoTone } from "@ant-design/icons-vue";
import TreeSelect from "@/components/TreeSelect";
import * as areaApi from "@/api/area";
import * as hospitalApi from "@/api/hospital";
import { message } from "ant-design-vue";
import { useRouter } from "vue-router";
import { guid } from "@/utils/validate";
export default {
  components: {
    TreeSelect,
    EnvironmentTwoTone,
  },
  setup() {
    const router = useRouter();
    const validateUserPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入手机号"));
      } else if (value && !/^1(3|4|5|6|7|8|9)\d{9}$/.test(value)) {
        callback(new Error("电话有误，请重填"));
      } else {
        callback();
      }
    };
    const validateUserPwd = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入新密码"));
      } else if (value.length < 6 || value.length > 20) {
        callback(new Error("请输入6-20位新密码"));
      } else if (
        formState.passwordComfirm &&
        value != formState.passwordComfirm
      ) {
        callback(new Error("两次密码输入不一致,请重新输入"));
      } else {
        callback();
      }
    };
    const validateUserComPwd = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入确认新密码"));
      } else if (value.length < 4 || value.length > 20) {
        callback(new Error("请输入4-20位确认新密码"));
      } else if (value != formState.password) {
        callback(new Error("两次密码输入不一致,请重新输入"));
      } else {
        callback();
      }
    };
    const rules = {
      mobile: [
        {
          required: true,
          validator: validateUserPhone,
          trigger: "blur",
        },
      ],
      randomValidateCode: [
        {
          required: true,
          message: "请输入图形验证码",
          trigger: "blur",
        },
      ],
      smsCode: [
        {
          required: true,
          message: "请输入短信验证码",
          trigger: "blur",
        },
      ],
      password: [
        {
          required: true,
          validator: validateUserPwd,
          trigger: "blur",
        },
      ],
      passwordComfirm: [
        {
          required: true,
          validator: validateUserComPwd,
          trigger: "blur",
        },
      ],
    };
    const formRef = ref();
    const formState = reactive({
      mobile: "", //手机号
      randomValidateCode: "",
      smsCode: "", //
      password: "", //
      passwordComfirm: "", //
    });
 //获取图形验证码
 const src = ref('')
    const handleGetRandomValidateCode =()=>{
      let uuid = guid();
      src.value = `/waste/sysUser/randomValidateCode??uuid=${uuid}`; // 将转换出来的路径赋值给变量，其实和上一步结合就可以
    }
    const handleSave = () => {
      formRef.value.validate().then((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(formState));
          delete params.passwordComfirm;
          userApi.modifyPasswordWithRandomValidateCode(params).then((res) => {
            if (res && res.success) {
              router.go(-1);
            }
          });
        }
      });
    };

    const backLogin = () => {
      router.go(-1);
    };

    // 获取验证码
    const showTime = ref(false);
    const timer = ref(60);
    const getCode = () => {
      if (showTime.value) {
        return;
      }
      if (!formState.mobile) {
        message.error("请输入手机号");
        return;
      } else if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(formState.mobile)) {
        message.error("电话有误，请重填");
        return;
      }
      timer.value = 60;
      showTime.value = true;
      let timertime = null;
      if (timertime) {
        clearInterval(timertime);
      }
      userApi.sendMessage({ phone: formState.mobile }).then((res) => {
        if (res && res.success) {
          timertime = setInterval(() => {
            if (timer.value <= 0) {
              showTime.value = false;
              clearInterval(timertime);
            } else {
              timer.value--;
            }
          }, 1000);
        }
      });
    };
    onMounted(async () => {
      handleGetRandomValidateCode()
    });
    return {
      formRef,
      router,
      rules,
      wrapperCol: {
        span: 16,
      },
      labelCol: {
        style: {
          width: "150px",
        },
      },
      formState,

      getCode,
      showTime,
      timer,
      handleSave,
      handleGetRandomValidateCode,
      backLogin,
      src
    };
  },
};
</script>
<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  .el-tabs__item {
    color: #fff;
  }
  .el-input {
    display: inline-block;
    // height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-form-item {
    margin-bottom: 24px;
    position: relative;
  }
  .el-form-item-1 {
    border: 1px solid #d7dadb;
    background: #fff;
    border-radius: 2px;
    height: 40px;
    line-height: 40px;
    color: #a5a7a8;
  }
  .login-form-content {
    width: 800px;
    min-height: 560px;
    display: flex;
    margin: 0 auto;
    padding: 45px 0;
    align-items: center;
    .login-bg {
      width: 800px;
      background: #fff;
      padding: 40px;
      border-radius: 8px;
    }
  }
}
</style>

<style lang="scss" scoped>
.demo-ruleForm {
  margin: 0 auto;
}
$bg: #34bbff;
$dark_gray: #333;
$light_gray: #333;
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  background: url("../../assets/bg.png") center center no-repeat;
  background-size: cover;
  overflow: hidden;
  display: flex;
  align-items: center;

  .login-form {
    position: relative;
    max-width: 100%;
    overflow: hidden;
    box-sizing: border-box;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    height: 40px;
    line-height: 40px;
    box-sizing: border-box;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color: $light_gray;
      text-align: center;
      font-weight: bold;
      margin-top: 52px;
    }
    .titleEn {
      font-size: 12px;
      color: $light_gray;
      margin: 10px 0;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .show-pwd-code {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: #34bbff;
    cursor: pointer;
    user-select: none;
  }
}
.form-rright {
  background: #fff;
  margin: 10px 0;
  color: $light_gray !important;
  flex-direction: column;
  padding: 0 70px;
  box-sizing: border-box;
  width: 440px;
  border-top-right-radius: 8px;
  border-bottom-right-radius: 8px;
}
.el-tabs {
  width: 100%;
  ::v-deep .el-tabs__nav {
    width: 100%;
  }
  ::v-deep .el-tabs__item {
    width: 50% !important;
    color: #a5a7a8 !important;
    padding: 0;
    font-size: 16px;
    text-align: center;
  }
  ::v-deep .el-tabs__active-bar {
    width: 50% !important;
    background: #34bbff;
  }
  ::v-deep .is-active {
    color: #34bbff !important;
  }
  ::v-deep .el-form-item__content {
    height: 38px;
    line-height: 38px;
  }
  ::v-deep .el-input {
    height: 38px;
    line-height: 38px;
    input {
      height: 38px;
      line-height: 38px;
      box-sizing: border-box;
      color: #a5a7a8;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #7c7e82 !important;
      }
    }
  }
  .svg-icon {
    vertical-align: 0.35em;
  }
}
.color-gray {
  color: #a5a7a8 !important;
}
::v-deep .el-tabs__header {
  margin-bottom: 30px;
}
.title {
  font-weight: bold;
  color: #333;
}
.back {
  font-size: 14px;
  color: #a5a7a8;
}
.sub-title {
  font-size: 16px;
  color: #333;
}
</style>
